#ifndef START_ANIMATION_H
#define START_ANIMATION_H

#include "engine.h"
#include "utils.h"
#include "../gui_base.h"

class StartAnimation: public GuiBase, public AnimationObserver
{
public:
    StartAnimation(Engine* engine, int w_width, int w_height, int duration)
        :GuiBase(engine), penColor(QColor(0, 0, 255, 0)), font1("Palatino", 12), font2("Palatino", 11)
    {
        x_start = w_width/2.5;
        y_start = w_height/3.5;
        alpha = alpha2 = length = 0;
        reversed = false;

        sx = x_start - 3;
        sy = y_start - 30;
        sw = w_width - w_width/2.5 + 10;
        sh = 150;

        a1 = new Animation(engine->fps, duration);
        a1->AddObserver(this);

        int* vArr[4] = {&length, &alpha, &alpha2, &src};
        string names[4] = {"length", "alpha", "alpha2", "src"};
        ig.AddSimpleProperties(names, vArr, 4, &invalidated);
        ig.AddInterpolator(a1->GetTime(), 0, duration/3, "alpha", 0, 255);
        ig.AddInterpolator(a1->GetTime(), 0, duration/3, "length", 0, w_width - w_width/2.5);
        ig.AddInterpolator(a1->GetTime(), 0, duration/3, "alpha2", 0, 255);

        engine->AddAnimation(a1);
    }

    virtual ~StartAnimation(){}

    virtual void Draw(QPainter &widget_painter)
    {
        penColor.setBlue(255);
        penColor.setAlpha(alpha);
        widget_painter.setFont(font1);
        widget_painter.setPen(penColor);
        widget_painter.drawText(x_start, y_start - 2, QString("Welcome to Music Practice"));
        widget_painter.drawLine(x_start, y_start, x_start + length, y_start);

        if(alpha2 > 0)
        {
            penColor.setBlue(0);
            penColor.setAlpha(alpha2);
            widget_painter.setFont(font2);
            widget_painter.setPen(penColor);
            widget_painter.drawText(x_start, y_start + 30, QString("Go to menu to begin"));
        }
        invalidated = false;
    }

    void Start()
    {
        a1->Play();
    }

    virtual void OnPause(Animation* a){};
    virtual void OnStop(Animation* a){};
    virtual void OnEnd(Animation* a)
    {
        if(!reversed)
        {
            reversed = true;
            a1->Reverse(true);
            a1->Play();
        } else
        {
            engine->RemoveAnimation(a1);
        }
    };
    virtual void OnPlay(Animation* a){};

private:
    int x_start, y_start, length;
    int alpha, src, alpha2;

    Animation* a1;
    IntGraph ig;

    QColor penColor;
    QFont font1;
    QFont font2;
    bool reversed;
};

#endif // START_ANIMATION_H
